<?php

namespace Parser\Separator;

use Parser\Analysis;
use Parser\Enquiry;
use Parser\Parser;
use Parser\Repetition;

class PingAn implements BaseSeparator
{

    public static $ApiKindList = [
        3000 => 'CV01001',
        30001 => 'CV27027',
        3001 => 'CV08000',
        3002 => 'CV17000',
        30021 => 'CV56075',
        3003 => 'CV18000',
        30031 => 'CV58077',
        3004 => 'CV36041',
        30041 => 'CV60079',
        4000 => 'CV05002',
        40001 => 'CV31028',
        4001 => 'CV74000',
        5001 => 'CV17005',
        50011 => 'CV44080',
        5002 => 'CV13004',
        50021 => 'CV44049',
        6000 => 'CV09003',
        60001 => 'CV41048',
        7001 => 'CV48057',
        7002 => 'CV49063',
    ];

    public function interpreter(array $array, $parser)
    {
        if ($this->error($array, $parser)) {
            $parser->result = (new Enquiry())->get();
            return;
        }

        $parser->result = $this->analysis($array);
    }

    /**
     *错误解析
     * Author:ls
     *
     * @param array $array
     * @param parser $parser
     * @return bool
     */
    public function error(&$array, $parser): bool
    {

        if ($resultCode = $array['resultCode'] ?? false and $resultCode == '500') {
            $internetFilter = $array['internetFilter'] ?? '';

            $parser->message = $internetFilter ? ($internetFilter . '--' . ($array['resultMessage'] ?? '')) : ($array['resultMessage'] ?? '');
            $parser->code = '500';
            return true;
        }

        if ($circMessage = $array['c51CaculateResult']['c51ResultDTO']['circMessage'] ?? false and $circMessage != '成功') {
            $parser->message = $circMessage;
            $parser->code = '500';
            return true;
        }
        //        echo json_encode($array);exit;
        if ($modifyAdvise = $array['c01CaculateResult']['c01ResultDTO']['decisionTreeResult']['modifyAdvise'] ?? '') {
            $parser->message = $modifyAdvise;
            $parser->code = '500';
            return true;
        }

        if (($repeatList = ($array['applyQueryResult']['circInfoDTO']['thirdVehicleReinsureList'] ?? []) and count($repeatList) >= 1)) {
            $repeater = $repeatList[0];
            $parser->message = '商业险重复投保,终保日期:' . ($repeater['dateExpire'] ?? '');
            $parser->code = '500';
            return true;
        }

        if ($processType = $array['processType'] ?? '') {
            switch ($processType) {
                case 'renewalAndC51DisTax';
                    $parser->message = '本次投保保险起期大于上年保单止期，请确认本年保险起止日期  交强险:' . $array['voucher']['c51BaseInfo']['insuranceBeginTime'] ?? '';
                    $parser->code = '500';
                    return true;
                case 'chooseVehType':
                    $parser->message = $array['applyQueryResult']['errorMessage'] ?? false;
                    if ($parser->message == '成功') {
                        $parser->message = '车型选择异常';
                    }
                    $parser->code = '500';
                    return true;
            }
        }


        return false;
    }

    public function analysis($array): array
    {
        $enquiry = new Enquiry();

        $enquiry->setTaxTotal($array['voucher']['vehicleTaxInfo']['totalTaxMoney'] ?? '0');
        $enquiry->setTaxPremium($array['voucher']['vehicleTaxInfo']['totalTaxMoney'] ?? '0');

        $enquiry->setTCITotal($array['c51CaculateResult']['c51ResultDTO']['totalActualPremium'] ?? '0');
        $enquiry->setTCIPremium($array['c51CaculateResult']['c51ResultDTO']['totalStandardPremium'] ?? '0');

        $enquiry->setVCITotal($array['voucher']['c01BaseInfo']['totalActualPremium'] ?? '0');
        $enquiry->setVCIPremium($array['voucher']['c01BaseInfo']['totalStandardPremium'] ?? '0');

        if ($enquiry->getVCITotal() != 0) {
            $enquiry->setVCIDiscount(number_format($enquiry->getVCITotal() / $enquiry->getVCIPremium(), 4));
        }

        $enquiry->setTotal();

        if (($repeatList = ($array['applyQueryResult']['circInfoDTO']['thirdVehicleReinsureList'] ?? []) and count($repeatList) >= 1)) {
            $repeater = $repeatList[0];
            $enquiry->failure();
            $enquiry->setErrorTip('商业险重复投保,终保日期:' . ($repeater['dateExpire'] ?? ''));
            return $enquiry->get();
        }

        if (isset($array['voucher']['c01DutyList'])) {
            foreach ($array['voucher']['c01DutyList'] as $kindList) {
                $code = (string)array_search($kindList['dutyCode'], self::$ApiKindList);
                if (!$code) {
                    continue;
                }
                $coverages['code'] = (string)array_search($kindList['dutyCode'], self::$ApiKindList);
                $coverages['shortName'] = $kindList['dutyName'] ?? array_search($code, analysis::$kindNameList);
                $coverages['insuredDesc'] = (string)$kindList['insuredAmount'] ?? '';
                $coverages['total'] = (string)$kindList['totalActualPremium'];
                $enquiry->setVCICoverages($coverages);
            }
        }

        //商业险费率因子
        if ($array['voucher'] ?? false) {
            if ($c01DisplayRateFactorList = $array['voucher']['c01DisplayRateFactorList'] ?? false) {
                foreach ($c01DisplayRateFactorList as $factorList) {
                    //自主定价系数
                    if ($factorCode = $factorList['factorCode'] ?? false and $factorCode == 'F78') {
                        $enquiry->setIndependentPriceRatio($factorList['factorRatioCOM']);
                    }
                    //无赔款优待系数F30
                    if ($factorCode = $factorList['factorCode'] ?? false and $factorCode == 'F30') {
                        $enquiry->setNCDRate($factorList['factorRatioCOM'] ?? '0');
                    }
                    //交通违法系数F34;车损险绝对免赔额系数F15
                }
            }

        }

        $enquiry->success();
        return $enquiry->get();
    }

    public function repetition($array): Repetition
    {
        return new Repetition();
    }
}

